package com.jcfk.system.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.common.cache.redis.service.RedisStringService;
import com.jcfk.common.core.context.SpringContextUtils;
import com.jcfk.common.security.utils.SecurityUtils;
import com.jcfk.system.domain.model.SendMsgModel;
import com.jcfk.system.domain.po.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 发送企业微信推送
 */
@Component("callPhoneService")
public class CallPhoneService {
    private static SysMessageSendUserService sysMessageSendUserService;


    private static SysMessageSendRecordService sysMessageSendRecordService;


    private static SysMessageSendRecordDetailsService sysMessageSendRecordDetailsService;

    private static SysPhoneCallService sysPhoneCallService;

    /**
     * 多级分类服务
     */


    // 使用静态代码块及SpringContextUtils获取对象
    static {
        sysMessageSendUserService = SpringContextUtils.getBean(SysMessageSendUserService.class);
        sysMessageSendRecordService = SpringContextUtils.getBean(SysMessageSendRecordService.class);
        sysMessageSendRecordDetailsService = SpringContextUtils.getBean(SysMessageSendRecordDetailsService.class);
        sysPhoneCallService = SpringContextUtils.getBean(SysPhoneCallService.class);
    }

    public void callPhone(SendMsgModel message, SysMessageSendRecord record) {

        // 接收人
        QueryWrapper<SysMessageSendUser> sysMessageSendUserQueryWrapper = new QueryWrapper<>();
        sysMessageSendUserQueryWrapper.lambda().eq(SysMessageSendUser::getTempleId, record.getTempleId()).eq(SysMessageSendUser::getDeleted, 0).eq(SysMessageSendUser::getType, 4);
        List<SysMessageSendUser> list = sysMessageSendUserService.list(sysMessageSendUserQueryWrapper);

        // 用户id
        List<String> phones = list.stream().map(SysMessageSendUser::getPhone).collect(Collectors.toList());
        phones.addAll(message.getReceivePhones());
        phones.stream().distinct().collect(Collectors.toList());

        List<SysMessageSendRecordDetails> records = new ArrayList<>();
        List<SysPhoneCall> phoneCalls = new ArrayList<>();
        // 发送记录入库
        for (String phone : phones) {
            SysMessageSendRecordDetails details = new SysMessageSendRecordDetails();
            details.setPhone(phone);
            details.setTitle(record.getTitle());
            details.setContent(message.getMsgBody());
            details.setMessageId(record.getId());
            details.setCreatedUserId(record.getCreatedUserId());
            details.setCreateTime(new Date());
            details.setCreatedUserId(record.getCreatedUserId());
            details.setCreatedUserName(record.getCreatedUserName());
            records.add(details);

            SysPhoneCall phoneCall = new SysPhoneCall();
            phoneCall.setCallTime(new Date());
            phoneCall.setTelephone(phone);
            phoneCall.setCreateTime(new Date());
            phoneCall.setVersion(1L);
            phoneCall.setLastUpdateTime(new Date());
            phoneCalls.add(phoneCall);

        }

        if (records.size() > 0) sysMessageSendRecordDetailsService.saveBigBatch(records);
        if (phoneCalls.size() > 0) sysPhoneCallService.saveBigBatch(phoneCalls);
        record.setMessageStatus(1);
        // 更新状态
        sysMessageSendRecordService.updateById(record);

    }

}
